package com.example.my_model;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.example.common.mvvm.repository.BaseRepository;
import com.example.my_model.accountentity.DataDTO;
import com.example.my_model.room.UserBean;
import com.example.my_model.room.UserDao;
import com.example.my_model.room.UserEntity;
import com.example.network_skd.BaseEntity;

import java.util.List;

import io.reactivex.Maybe;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;


public class UserRepository extends BaseRepository {
    public Observable<BaseEntity> getUser(UserDao userDao){
        Maybe<List<DataDTO>> listMaybe = userDao.queryUser();
        Observable<BaseEntity> baseEntityObservable = listMaybe.flatMapObservable(new Function<List<DataDTO>, ObservableSource<? extends BaseEntity>>() {
            @Override
            public ObservableSource<? extends BaseEntity> apply(@NonNull List<DataDTO> userEntities) throws Exception {
                UserBean userBean = new UserBean();
                userBean.status = 200;
                if (userEntities == null) {
                    userBean.message = "用户未登录";
                    Observable<UserBean> just = Observable.just(userBean);
                    return just;
                }
                if (userEntities.size() == 0) {
                    userBean.message = "用户未登录";
                    return Observable.just(userBean);
                }

                userBean.message = "用户已登录";
                userBean.data = userEntities.get(userEntities.size() - 1);
                Observable<UserBean> just = Observable.just(userBean);
                return observableFunc(just);
            }
        });
        return baseEntityObservable;
    }
}
